package com.sambo.inspection.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.JsonElement;
import com.sambo.inspection.R;
import com.sambo.inspection.activity.support.BaseActivity;
import com.sambo.inspection.adapter.CommonAdapter;
import com.sambo.inspection.adapter.ViewHolder;
import com.sambo.inspection.bean.InspectionPointBean;
import com.sambo.inspection.bean.PartitionBean;
import com.sambo.inspection.component.SlideFromTopPopup;
import com.sambo.inspection.http.HttpClient;
import com.sambo.inspection.http.HttpResponseHandler;
import com.sambo.inspection.http.Urls;
import com.sambo.inspection.util.JsonUtil;
import com.sambo.inspection.util.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InspectionPointSearchActivity extends BaseActivity implements View.OnClickListener {

    ImageView ivBack;
    EditText etInspectionPointCode;
    ImageView ivDelContent;
    LinearLayout llPosition;
    LinearLayout llInspectionCondition;
    TextView tvPosition;
    ListView lvInspectionPointList;

    private SlideFromTopPopup slideFromTopPopup;

    private CommonAdapter<InspectionPointBean> mAdapter;
    private List<InspectionPointBean> mInspectionPointDataList;
    private List<InspectionPointBean> mSrcInspectionPointDataList;

    private String mSelectCode;
    private String mSelectPartition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inspection_search);

        ivBack = findViewById(R.id.iv_back);
        etInspectionPointCode = findViewById(R.id.et_inspection_point_code);
        ivDelContent = findViewById(R.id.iv_del_content);
        llPosition = findViewById(R.id.ll_position);
        tvPosition = findViewById(R.id.tv_position);
        llInspectionCondition = findViewById(R.id.ll_inspection_condition);
        lvInspectionPointList = findViewById(R.id.lv_inspection_point_list);

        etInspectionPointCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mSelectCode = editable.toString();
                selectCheckPoint();
            }
        });

        mInspectionPointDataList = new ArrayList<>();
        mSrcInspectionPointDataList = new ArrayList<>();

        mAdapter = new CommonAdapter<InspectionPointBean>(this, mInspectionPointDataList, R.layout.item_inspection_point) {
            @Override
            public void convert(ViewHolder helper, InspectionPointBean item) {

                helper.setText(R.id.tv_inspection_point_name, item.getName());
                helper.setText(R.id.tv_inspection_point_code, String.valueOf(item.getCode()));

                if (TextUtils.equals("1", item.getDeviceType())) {
                    helper.setText(R.id.tv_eq_type, "流量计");
                } else if (TextUtils.equals("2", item.getDeviceType())) {
                    helper.setText(R.id.tv_eq_type, "压力表");
                } else if (TextUtils.equals("3", item.getDeviceType())) {
                    helper.setText(R.id.tv_eq_type, "水表");
                } else if (TextUtils.equals("4", item.getDeviceType())) {
                    helper.setText(R.id.tv_eq_type, "阀门");
                } else if (TextUtils.equals("5", item.getDeviceType())) {
                    helper.setText(R.id.tv_eq_type, "普通节点");
                } else {
                    helper.setText(R.id.tv_eq_type, "其他");
                }
                helper.setText(R.id.tv_partition, StringUtil.getSubName(item.getAddress(), 10));
            }
        };

        lvInspectionPointList.setAdapter(mAdapter);

        lvInspectionPointList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(InspectionPointSearchActivity.this, InspectionPointDetailsActivity.class);
                intent.putExtra("model", mInspectionPointDataList.get(i));
                startActivity(intent);
            }
        });

        slideFromTopPopup = new SlideFromTopPopup(this);
        slideFromTopPopup.setPartitionSelectListener(new SlideFromTopPopup.PartitionSelectListener() {
            @Override
            public void onSelectPartition(PartitionBean bean) {
                if (bean == null) {
                    tvPosition.setText("分区");
                    mSelectPartition = null;
                    selectCheckPoint();
                } else {
                    tvPosition.setText(bean.getDeptName());
                    mSelectPartition = bean.getDeptName();
                    selectCheckPoint();
                }
            }
        });

        ivBack.setOnClickListener(this);
        llPosition.setOnClickListener(this);

        getPartitionInfo();
        getInspectionInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_position:
                slideFromTopPopup.showPopupWindow(llInspectionCondition);
                break;
        }
    }

    private void getPartitionInfo() {

        Map<String, String> params = new HashMap<>();

        params.put("page", "1");
        params.put("limit", "200");

        HttpClient.get(this, Urls.API_SERVER_HOST + Urls.GET_DEPT_TREE_DATA, params,
                new HttpResponseHandler() {

                    @Override
                    public void onInnovationSuccess(JsonElement value) {
                        super.onInnovationSuccess(value);
                        PartitionBean[] response = JsonUtil.toObj(value.toString(), PartitionBean[].class);

                        if (response != null && response.length > 0) {
                            Map<String, String> searchLeafMap = new HashMap<>();

                            for (PartitionBean treePoint : response) {
                                searchLeafMap.put(treePoint.getPid(), treePoint.getId());
                            }

                            for (PartitionBean treePoint : response) {
                                if (searchLeafMap.containsKey(treePoint.getId())) {
                                    treePoint.setISLEAF("0");
                                } else {
                                    treePoint.setISLEAF("1");
                                }
                            }
                            slideFromTopPopup.setTreeData(Arrays.asList(response));
                        }

                    }
                }, null);
    }

    private void getInspectionInfo() {

        Map<String, String> params = new HashMap<>();
        if (!TextUtils.isEmpty(etInspectionPointCode.getText().toString())) {
            params.put("code", etInspectionPointCode.getText().toString());
        }
        if (!TextUtils.equals(tvPosition.getText(), "分区")) {
            params.put("deptId", etInspectionPointCode.getText().toString());
        }
        params.put("page", "1");
        params.put("limit", "100");

        HttpClient.get(this, Urls.API_SERVER_HOST + Urls.GET_CHECK_POINT, params,
                new HttpResponseHandler() {

                    @Override
                    public void onInnovationSuccess(JsonElement value) {
                        super.onInnovationSuccess(value);
                        mInspectionPointDataList.clear();
                        mSrcInspectionPointDataList.clear();

                        InspectionPointBean[] response = JsonUtil.toObj(value.toString(), InspectionPointBean[].class);

                        if (response != null && response.length > 0) {
                            mInspectionPointDataList.addAll(Arrays.asList(response));
                            mSrcInspectionPointDataList.addAll(mInspectionPointDataList);
                        }

                        mAdapter.notifyDataSetChanged();
                    }
                }, null);
    }

    private void selectCheckPoint() {

        mInspectionPointDataList.clear();

        List<InspectionPointBean> selectList = new ArrayList<>();

        if (TextUtils.isEmpty(mSelectCode)) {
            selectList.addAll(mSrcInspectionPointDataList);
        } else {
            for (InspectionPointBean bean : mSrcInspectionPointDataList) {
                if (mSelectCode.startsWith(String.valueOf(bean.getCode()))) {
                    selectList.add(bean);
                }
            }
        }

        if (TextUtils.isEmpty(mSelectPartition)) {
            mInspectionPointDataList.addAll(selectList);
        } else {
            for (InspectionPointBean bean : selectList) {
                if (TextUtils.equals(bean.getBelongRegion(), mSelectPartition)) {
                    mInspectionPointDataList.add(bean);
                }
            }
        }

        mAdapter.notifyDataSetChanged();
    }
}
